package com.karl.loading.loadings;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.karl.loading.base.BaseProgressView;

/**
 * Created by CoderFan on 2017/8/27.
 * desc:仿小密圈下拉控件
 */

public class MiLoadingView extends BaseProgressView{

    private Paint mPaint;

    private RectF mRectF;

    private float scales;

    private float rotates;

    public MiLoadingView(Context context) {
        this(context,null);
    }

    public MiLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        setMaxProgress(1);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(dp2px(DEFAULT_STROKE_WIDTH));
        mPaint.setColor(Color.parseColor("#CDC9C9"));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth()/2;
        int y = getHeight()/2;

        canvas.translate(x,y);
        if (mRectF == null){
            mRectF = new RectF(-x+ defaultPadding,-y+ defaultPadding,x- defaultPadding,y- defaultPadding);
        }
        mPaint.setStyle(Paint.Style.STROKE);
        switch (getCurrentMode()){
            case MODE_PROGRESS:
                canvas.scale(scales,scales);
                canvas.drawCircle(0,0,x- defaultPadding,mPaint);
                break;
            case MODE_LOADING:
                canvas.rotate(rotates);
                canvas.drawArc(mRectF,270,-300,false,mPaint);

                mPaint.setStyle(Paint.Style.FILL);
                double angle = -60.0;
                double radians = Math.toRadians(angle);
                float cx = (float) ((x- defaultPadding)*Math.cos(radians));
                float cy = (float) ((y- defaultPadding)*Math.sin(radians));
                int radius = dp2px(DEFAULT_STROKE_WIDTH+1);
                canvas.drawCircle(cx,cy,radius,mPaint);
                break;
        }
    }

    @Override
    public void updateProgress(float progress) {
        super.updateProgress(progress);
        if (scales > getMaxProgress()){
            scales = getMaxProgress();
        }
        scales = progress;
        postInvalidate();
    }

    @Override
    public void setColor(int color) {
        mPaint.setColor(color);
        postInvalidate();
    }

    @Override
    protected ValueAnimator initAnim() {
        return ValueAnimator.ofFloat(0,360);
    }

    @Override
    public void startAnim() {
        startAnim(500);
    }

    @Override
    public void startAnim(long time) {
        startAnim(
                ValueAnimator.INFINITE,
                ValueAnimator.RESTART,
                time
        );
    }

    @Override
    protected void onUpdateAnim(ValueAnimator animator) {
        rotates = (float) animator.getAnimatedValue();
        postInvalidate();
    }


}
